package com.itheima.d1_Api_Thread;

/**
 * Api:
 *      1.setName
 *      2.getName()
 *      2.Thread.currentThread()//静态方法
 */
public class demo1 {
    public static void main(String[] args) {
        Thread t1 = new MyThread("1号");
        Thread t2 = new MyThread("2号");
       //不设置，则使用默认线程名字：Thread-0,1,2,3...

        //使用父类有参构造器来setName更好
//        t1.setName("p1");
//        t2.setName("p2");
        t1.start();
        t2.start();

        //main线程
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"输出了："+ i);
        }

    }
}

class MyThread extends Thread{//自定义线程类需要继承线程类Thread
    private String name;
//使用super来设置名字
    public MyThread(String name) {
        super(name);
    }
    public MyThread() {

    }

    @Override
    public void run(){
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"输出了："+ i);
        }
    }
}
